package com.ta.gulimall.ware.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ta.gulimall.common.utils.system.PageUtils;
import com.ta.gulimall.common.utils.system.Query;

import com.ta.gulimall.ware.dao.PurchaseDetailDao;
import com.ta.gulimall.ware.view.entity.PurchaseDetailEntity;
import com.ta.gulimall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        String status = (String) params.get("status");
        String wareId = (String) params.get("wareId");
        LambdaQueryWrapper<PurchaseDetailEntity> wrapper = new LambdaQueryWrapper<PurchaseDetailEntity>()
                .and(StrUtil.isNotBlank(key), wr -> wr.eq(PurchaseDetailEntity::getPurchaseId, key).or().eq(PurchaseDetailEntity::getSkuId, key))
                .eq(StrUtil.isNotBlank(status), PurchaseDetailEntity::getStatus, status)
                .eq(StrUtil.isNotBlank(wareId), PurchaseDetailEntity::getWareId, wareId);
        IPage<PurchaseDetailEntity> page = this.page(new Query<PurchaseDetailEntity>().getPage(params), wrapper);

        return new PageUtils(page);
    }

    @Override
    public List<PurchaseDetailEntity> listPurchaseDetail(List<Long> purchaseIds) {
        ArrayList<PurchaseDetailEntity> list = new ArrayList<>();
        purchaseIds.forEach(m -> {
            LambdaQueryWrapper<PurchaseDetailEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PurchaseDetailEntity::getPurchaseId, m);
            list.addAll(list(wrapper));
        });

        return list;
    }

}